﻿Public Class Dnotaria

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of Enotaria)
        Dim notarias As List(Of Enotaria) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_notaria_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            notarias = New List(Of Enotaria)
                            Dim notaria As Enotaria = Nothing
                            Do While dr.Read
                                notaria = New Enotaria( _
                                    dr.GetInt32(dr.GetOrdinal("idNotaria")), _
                                    dr.GetString(dr.GetOrdinal("descNotaria")), _
                                    dr.GetString(dr.GetOrdinal("nomContacto")), _
                                    dr.GetString(dr.GetOrdinal("apePaterContac")), _
                                    dr.GetString(dr.GetOrdinal("apeMaterContac")), _
                                    dr.GetString(dr.GetOrdinal("telefono")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                notarias.Add(notaria)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return notarias
    End Function
    Public Shared Function SeleccionarXEstado(ByVal descEmpresa As String) As List(Of Enotaria)
        Dim notarias As List(Of Enotaria) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_notaria_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            notarias = New List(Of Enotaria)
                            Dim notaria As Enotaria = Nothing
                            Do While dr.Read
                                notaria = New Enotaria( _
                                    dr.GetInt32(dr.GetOrdinal("idNotaria")), _
                                    dr.GetString(dr.GetOrdinal("descNotaria")), _
                                    dr.GetString(dr.GetOrdinal("nomContacto")), _
                                    dr.GetString(dr.GetOrdinal("apePaterContac")), _
                                    dr.GetString(dr.GetOrdinal("apeMaterContac")), _
                                    dr.GetString(dr.GetOrdinal("telefono")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                notarias.Add(notaria)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return notarias
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pNotaria As Enotaria) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_notaria_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@descNotaria", SqlDbType.VarChar, 55).Value = pNotaria.descNotaria
                cmd.Parameters.Add("@nomContacto", SqlDbType.VarChar, 55).Value = pNotaria.nomContacto
                cmd.Parameters.Add("@apePaterContac", SqlDbType.VarChar, 55).Value = pNotaria.apePaterContac
                cmd.Parameters.Add("@apeMaterContac", SqlDbType.VarChar, 55).Value = pNotaria.apeMaterContac
                cmd.Parameters.Add("@telefono", SqlDbType.VarChar, 11).Value = pNotaria.telefono
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pNotaria.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal pNotaria As Enotaria) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_notaria_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idNotaria", SqlDbType.Int).Value = pNotaria.idNotaria
                cmd.Parameters.Add("@descNotaria", SqlDbType.VarChar, 55).Value = pNotaria.descNotaria
                cmd.Parameters.Add("@nomContacto", SqlDbType.VarChar, 55).Value = pNotaria.nomContacto
                cmd.Parameters.Add("@apePaterContac", SqlDbType.VarChar, 55).Value = pNotaria.apePaterContac
                cmd.Parameters.Add("@apeMaterContac", SqlDbType.VarChar, 55).Value = pNotaria.apeMaterContac
                cmd.Parameters.Add("@telefono", SqlDbType.VarChar, 11).Value = pNotaria.telefono
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = pNotaria.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pNotaria.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal pNotaria As Enotaria) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_notaria_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idNotaria", SqlDbType.Int).Value = pNotaria.idNotaria
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pNotaria.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
